package com.cheyun.netsalev3.view

import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.RecyclerView
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.MyPagedListAdapter
import com.cheyun.netsalev3.bean.OutlistParam
import com.cheyun.netsalev3.databinding.ActivityOverdueBinding
import com.cheyun.netsalev3.utils.BaseActivity
import com.cheyun.netsalev3.utils.InjectorUtils
import com.cheyun.netsalev3.utils.api.NetworkState
import com.cheyun.netsalev3.viewmodel.OverdueActivityViewModel

class OverdueActivity : BaseActivity<ActivityOverdueBinding>() {
    private lateinit var adapter: MyPagedListAdapter<OutlistParam>
    override fun initContentView(): Int {
        return R.layout.activity_overdue
    }

    //逾期state：1 七日强转2
    var state = 1

    override fun initViewModel(): ViewModel? {
        state = intent.getIntExtra("state", 1)
        var factory = InjectorUtils.getOverdueActivityViewModelFactory(state)
        return ViewModelProviders.of(this, factory).get(OverdueActivityViewModel::class.java)
    }

    override fun initData() {
        binding.viewModel?.listdata?.observe(this, Observer { list ->
            adapter.submitList(list)
        })
        //逾期state：1 七日强转2
        when (state) {
            1 -> binding.headview.titleText?.text = "逾期"
            2 -> binding.headview.titleText?.text = "七日强转"
        }

        binding.viewModel?.networkState?.observe(this, Observer {
            binding.swipeRefreshLayout.isRefreshing = it == NetworkState.LOADING
            adapter.setNetworkState(it)
        })
    }


    override fun initView() {
        adapter = MyPagedListAdapter(R.layout.overdue_item, binding.viewModel)
        var recyclerView = binding.root.findViewById<RecyclerView>(R.id.swipe_target)
        recyclerView.adapter = adapter
        binding.swipeRefreshLayout.setOnRefreshListener {
            binding.viewModel?.refresh()
        }

    }
}
